package com.store.controller;

import com.store.entity.Address;
import com.store.service.impl.AddressServiceImpl;
import com.store.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cai
 * @since 2022-07-12
 */
@RestController
@RequestMapping("address")
public class AddressController extends BaseController{
    @Autowired
    AddressServiceImpl addressService;

    @RequestMapping("add_new_address")
    public JsonResult<Void> getUserInfoByUid(Address address, HttpSession session){
        //调用session.getAttribute("")获取uid
        Integer uid= getUidFromSession(session);
        String username= getUsernameFromSession(session);
        // 调用业务执行获取对象部分信息
        addressService.addAddress(uid,username,address);
        //返回成功
        return new JsonResult<Void>(OK);
    }

    @RequestMapping({"","/"})
    public JsonResult<List<Address>> getByUid(HttpSession session){
        //调用session.getAttribute("")获取uid
        Integer uid= getUidFromSession(session);
        // 调用业务执行获取对象部分信息
        List<Address> data= addressService.selectAllAddress(uid);
        //System.out.println(data);
        //返回成功
        return new JsonResult<>(OK,data);
    }

    @RequestMapping("{aid}/set_default")
    public JsonResult<Void> setDefault( @PathVariable("aid") Integer aid,HttpSession session){
        //调用session.getAttribute("")获取uid
        Integer uid= getUidFromSession(session);
        // 调用业务执行
        addressService.setDefault(aid,uid);
        //返回成功
        return new JsonResult<Void>(OK);
    }

    @RequestMapping("{aid}/delete")
    public JsonResult<Void> deleteAddress( @PathVariable("aid") Integer aid,HttpSession session){
        //调用session.getAttribute("")获取uid
        Integer uid= getUidFromSession(session);
        // 调用业务执行删除
        addressService.deleteAddress(aid,uid);
        //返回成功
        return new JsonResult<Void>(OK);
    }

}
